<template>
  <div>
    <div class="bread">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/start' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a>博客</a></el-breadcrumb-item>
        <el-breadcrumb-item>博客操作</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="table">
      <el-table :data="tableData" stripe style="width: 100%" height="600">
        <el-table-column type="expand">
          <template slot-scope="props">
            <remark :id="props.row.id"></remark>
          </template>
        </el-table-column>
        <el-table-column
          v-for="item in tableItem"
          :key="item.id"
          :prop="item.prop"
          :label="item.label"
          :width="columWidth(item.prop)"
        >
          <template slot="header">
            <input
              type="text"
              class="searchInput"
              @input="chooseSearchBlog"
              placeholder="输入关键字搜索"
              v-model="searchBlog"
              v-if="item.prop == 'option'"
            />
            <span v-if="item.prop != 'option'">{{ item.label }}</span>
          </template>
          <template slot-scope="scope">
            <span
              class="limitWidth"
              @click="seeContent(scope.row)"
              :class="{ Active: item.prop === 'content' }"
              v-if="item.prop !== 'option' && item.prop !== 'time'"
              >{{ scope.row[item.prop] }}</span
            >
            <span v-if="item.prop === 'time'">{{
              scope.row[item.prop] | formatTime
            }}</span>
            <el-button
              type="danger"
              v-if="item.prop === 'option'"
              @click="startDel(scope.row.id)"
              >删除</el-button
            >
            <el-button
              type="primary"
              v-if="item.prop === 'option'"
              @click="startEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <transition name="fadeIn">
      <div v-if="showContent">
        <div class="black" @click="closeContent"></div>
        <div>
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>标题: {{ title }}</span>
              <span style="marginleft: 20px">点赞数: {{ goodNum }}</span>
              <span style="marginleft: 20px">观看数: {{ seeNum }}</span>
              <span style="marginleft: 20px">时间: {{ time | formatTime }}</span>
            </div>
            <div v-html="content">{{ content }}</div>
          </el-card>
        </div>
      </div>
    </transition>
    <blog-write
      class="write"
      ref="write"
      v-if="showWrite"
      @cancelEdit="cancelEdit"
    ></blog-write>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span class="error">确定删除该博客吗(不可改变)</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import format from "../../formatTime";
import remark from "./remark.vue";
import blogWrite from "./blogWrite.vue";
type TableItem = {
  id: string;
  prop: string;
  label: string;
};
type contentInfo = {
  seeNum: string;
  goodNum: string;
  time: string;
  title: string;
  content: string;
};
@Component({
  filters: {
    formatTime: (val: string) => {
      return format(val);
    },
  },
  components: {
    remark,
    blogWrite,
  },
})
export default class blog extends Vue {
  private tableData: any[] = [];
  private tableDataAll: any[] = [];
  private searchBlog: string = "";
  private showContent: boolean = false;
  private showWrite: boolean = false;
  private tableItem: TableItem[] = [
    { id: "0", prop: "id", label: "id" },
    { id: "1", prop: "title", label: "标题" },
    { id: "2", prop: "content", label: "内容" },
    { id: "3", prop: "time", label: "时间" },
    { id: "4", prop: "seeNum", label: "观看数" },
    { id: "5", prop: "goodNum", label: "点赞数" },
    { id: "6", prop: "way", label: "方式" },
    { id: "7", prop: "option", label: "操作" },
  ];
  private content: string = "";
  private seeNum: string = "";
  private goodNum: string = "";
  private title: string = "";
  private time: string = "";
  private id: number = 0;
  private dialogVisible: boolean = false;
  get columWidth() {
    return function (val: string) {
      if (val !== "time" && val !== "option" && val !== "content" && val !== "title") {
        return 100;
      }
    };
  }
  created() {
    this.getBlogList();
  }
  chooseSearchBlog() {
    this.tableData = this.tableDataAll.filter((item) => {
      let f = new RegExp(this.searchBlog, "g");
      return f.test(item.title);
    });
  }
  closeContent(): void {
    this.showContent = false;
  }
  startDel(val: number): void {
    this.id = val;
    this.dialogVisible = true;
  }
  startEdit(val: any): void {
    this.showWrite = true;
    console.log(val);

    Vue.nextTick(() => {
      (this.$refs.write as any).content = val.content;
      (this.$refs.write as any).way = val.way;
      (this.$refs.write as any).title = val.title;
      (this.$refs.write as any).id = val.id;
      (this.$refs.write as any).editContent = val.editContent;
    });
  }
  cancelEdit(item: any) {
    this.showWrite = false;
  }
  confirm(): void {
    this.$axios
      .post("/blog/del", {
        id: this.id,
        status: this.$store.state.user.userInfo.status,
      })
      .then((res) => {
        if (res.data.error === -1) {
          this.dialogVisible = false;
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.getBlogList();
        } else {
          this.dialogVisible = false;
          this.$message({
            type: "error",
            message: "删除失败",
          });
        }
      });
  }
  seeContent(val: contentInfo): void {
    this.content = val.content;
    this.seeNum = val.seeNum;
    this.goodNum = val.goodNum;
    this.title = val.title;
    this.time = val.time;
    this.showContent = true;
  }
  getBlogList() {
    this.$axios
      .get("/blog/alllist", {
        params: {
          status: this.$store.state.user.userInfo.status,
        },
      })
      .then((res) => {
        console.log(res);
        if (res.data.error === -1) {
          this.tableData = res.data.data;
          this.tableDataAll = res.data.data;
        } else {
          this.tableData = [];
        }
      });
  }
}
</script>

<style lang="less" scoped>
@import url("../../assets/css/animate.css");
.write {
  position: absolute;
  width: 100%;
  left: 0;
  top: 40px;
  z-index: 999;
}
.Active {
  transition: all 0.3s;
  width: 100%;
  height: 100%;
  &:hover {
    background-color: darkgrey;
    cursor: pointer;
  }
}
.bread {
  margin: 25px;
}
.table {
  box-sizing: border-box;
  padding: 5px 15px;
}
.searchInput {
  border-radius: 3px;
  border: 1px solid #dcdfe6;
  padding: 5px 10px;
  outline: none;
  &:focus {
    box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.6);
  }
}
.limitWidth {
  display: inline-block;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  line-height: 50px;
}
.box-card {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
  width: 60%;
  height: 500px;
  z-index: 9;
  overflow: scroll;
  .clearfix {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  span {
    margin: 0 10px;
  }
}
.black {
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  width: 100vw;
  height: 100vh;
  z-index: 2;
  background-color: rgba(0, 0, 0, 0.6);
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
